package woa.queue;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * 岛屿数量
 * 给你一个由 '1'（陆地）和 '0'（水）组成的的二维网格，请你计算网格中岛屿的数量。
 * 岛屿总是被水包围，并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
 * 此外，你可以假设该网格的四条边均被水包围。
 *
 * 输入：grid = [
 * ["1","1","1","1","0"],
 * ["1","1","0","1","0"],
 * ["1","1","0","0","0"],
 * ["0","0","0","0","0"]
 * ]
 * 输出：1
 *
 * m == grid.length
 * n == grid[i].length
 * 1 <= m, n <= 300
 * grid[i][j] 的值为 '0' 或 '1'
 *
 * @author wangpeng
 * @date 2021/11/24
 */
public class NumIslandsProblem {

    public static int numIslands(char[][] grid) {
        int count = 0;
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                char c = grid[i][j];
                if (c == '1') {
                    dfs(grid, m, n, i, j);
                    count++;
                }
            }
        }
        return count;
    }

    public static void bfs(char[][] grid, int m, int n, int i, int j) {
        Queue<int[]> queue = new ArrayDeque<>();
        queue.add(new int[]{i, j});
        grid[i][j] = '0';
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int p = 0; p < size; p++) {
                int[] arr = queue.poll();
                i = arr[0];
                j = arr[1];
                //right
                if (i + 1 < m && grid[i + 1][j] == '1') {
                    grid[i + 1][j] = '0';
                    queue.add(new int[]{i + 1, j});
                }
                //down
                if (j + 1 < n && grid[i][j + 1] == '1') {
                    grid[i][j + 1] = '0';
                    queue.add(new int[]{i, j + 1});
                }
                //up
                if (j - 1 >= 0 && grid[i][j - 1] == '1') {
                    grid[i][j - 1] = '0';
                    queue.add(new int[]{i, j - 1});
                }
                //left
                if (i - 1 >= 0 && grid[i - 1][j] == '1') {
                    grid[i - 1][j] = '0';
                    queue.add(new int[]{i - 1, j});
                }
            }
        }
    }

    public static void dfs(char[][] grid, int m, int n, int i, int j) {
        //right
        if (i + 1 < m && grid[i + 1][j] == '1') {
            grid[i + 1][j] = '0';
            dfs(grid, m, n, i + 1, j);
        }
        //down
        if (j + 1 < n && grid[i][j + 1] == '1') {
            grid[i][j + 1] = '0';
            dfs(grid, m, n, i, j + 1);
        }
        //up
        if (j - 1 >= 0 && grid[i][j - 1] == '1') {
            grid[i][j - 1] = '0';
            dfs(grid, m, n, i, j - 1);
        }
        //left
        if (i - 1 >= 0 && grid[i - 1][j] == '1') {
            grid[i - 1][j] = '0';
            dfs(grid, m, n, i - 1, j);
        }
    }

    public static void main(String[] args) {
        System.out.println(numIslands(new char[][]{{'1','0','0','1','0'},{'0','1','0','0','1'},{'1','0','1','0','0'},{'0','1','0','1','0'}}));
    }
}
